<!--
  ~ Copyright 2021 Red Hat, Inc. and/or its affiliates.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

{#if info:isProcessEnabled}
<a href="{urlbase}/processInstances" class="badge badge-light">
    <i class="fa fa-project-diagram fa-fw"></i>
    Process Instances <span id="processInstancesCount" class="badge badge-light"/>
</a>
<br/>
<a href="{urlbase}/tasks" class="badge badge-light">
    <i class="fa fa-tasks fa-fw"></i>
    Tasks <span id="tasksCount" class="badge badge-light"/>
</a>
<br/>
<a href="{urlbase}/jobs" class="badge badge-light">
    <i class="fa fa-clock fa-fw"></i>
    Jobs <span id="jobsCount" class="badge badge-light"/>
</a>
<br/>
<a href="{urlbase}/forms" class="badge badge-light">
    <i class="fab fa-wpforms fa-fw"></i>
    Forms <span id="formsCount" class="badge badge-light"/>
</a>
{/if}
{#if info:isTracingEnabled}
<a href="{urlbase}/audit" class="badge badge-light">
    <i class="fab fa-wpforms fa-fw"></i>
    Audit investigation <span id="auditCount" class="badge badge-light"/>
</a>
{/if}
{#if info:isProcessEnabled == false && info:isTracingEnabled == false}
<p>No Kogito addons detected.</p>
{/if}
{#if info:isProcessEnabled}
<script>
    const graphqlUrl = "{config:property('kogito.data-index.url') ?: ''}/graphql";

    {|
        const fillFormsCount = () => {
            fetch("/forms/count")
                .then(response => response.text())
                .then(count => $("#formsCount").text(count))
                .catch(error => {
                    console.log("Error while trying to fetch forms count.");
                    console.error(error);
                });
        };

        const fillGraphqlCount = (callback, graphql) => {
            fetch(graphqlUrl, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    Accept: "application/json",
                },
                body: JSON.stringify({ query: graphql }),
            })
                .then(r => r.json())
                .then(callback)
                .catch(error => {
                    console.log("Error while trying to fetch workflow instances count.");
                    console.error(error);
                });
        };

        fillGraphqlCount(function (json) {
            $("#processInstancesCount").text(json.data.ProcessInstances.length)
        }, "query getAllProcessesIds{  ProcessInstances{ id } }");

        fillGraphqlCount(function (json) {
            $("#tasksCount").text(json.data.UserTaskInstances.length)
        }, "query getAllTasksIds{  UserTaskInstances{ id } }");

        fillGraphqlCount(function (json) {
            $("#jobsCount").text(json.data.Jobs.length)
        }, "query getAllJobsIds{  Jobs{ id } }");

        fillFormsCount("forms");
    |}
</script>
{/if}